#include <Python.h>
#include <stdio.h>
#include <stdlib.h>

static PyObject * spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return Py_BuildValue("i", sts);
}

static float list_add(float *f, int size)
{
    float sum = 0;
    int i = 0;
    for (i=0; i<size; i++)
    {
        sum += f[i];
    }
    return sum;
}

static PyObject * hy_listadd(PyObject *self, PyObject *args)
{
    PyObject *list;
    PyObject *temp;

    int len;
    if (!PyArg_ParseTuple(args, "O", &temp)) return NULL;
    len = PyList_GET_SIZE(temp);
    float *f = (float *)malloc(len*sizeof(float));
    printf("the len is %d\n",len);
    int i;
    for(i=0;i <len;++i)
    {
        list = PyList_GetItem(temp,i);
        f[i] = PyFloat_AsDouble(list);
    }
    float value = list_add(f, len);
    free(f);
    return Py_BuildValue("f", value);
}

static PyMethodDef moduleMethods[] =
{
     {"system", spam_system, METH_VARARGS, "Greet somebody."},
     {"listadd", hy_listadd, METH_VARARGS},
     {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initspam(void)
{
     (void) Py_InitModule("spam", moduleMethods);
}

